import { CommontListResp } from '@/api/types'
import {
  MaterialItem,
  MaterialStatus,
} from '@/components/ItemList/MaterialList/types'
import config from '@/config'
import request, { ApiResponse } from '@/utils/service/request'
import { AuditLogItem } from '../auth/model/authorModel'
import {
  AsyncToPaixinReq,
  BatchAuditMaterialReq,
  QueryAuditMaterialRequest,
  UpdateMaterialStatusReq,
} from './model/materialModel'

export enum AuditMaterialApi {
  QUERY_MATERIAL = '/data-access/material/search',
  UPDATE_MATERIAL_STATUS = '/data-access/material/update/status',
  QUERY_MATERIAL_AUDIT_LOG = '/data-access/material/logs',
  /** 批量审核素材 */
  BATCH_AUDTI_MATERIAL = '/data-access/material/update/status/batch',
  /** 同步至拍信 */
  ASYNC_TO_PAIXIN = '/admin/medias/0/medias/add',
}

export function fetchAuditMaterials(body: QueryAuditMaterialRequest) {
  return request.post<ApiResponse<CommontListResp<MaterialItem>>>(
    AuditMaterialApi.QUERY_MATERIAL,
    body
  )
}

export function updateMaterialStatus(body: UpdateMaterialStatusReq) {
  return request.post<ApiResponse<null>>(
    AuditMaterialApi.UPDATE_MATERIAL_STATUS,
    body
  )
}

export function queryMaterialAuditLog(id: string) {
  return request.get<ApiResponse<AuditLogItem<MaterialStatus>[]>>(
    AuditMaterialApi.QUERY_MATERIAL_AUDIT_LOG,
    {
      params: { id },
    }
  )
}

export function batchAuditMaterial(body: BatchAuditMaterialReq) {
  return request.post<ApiResponse<null>>(
    AuditMaterialApi.BATCH_AUDTI_MATERIAL,
    body
  )
}

export function upToPaixin(accessList: AsyncToPaixinReq[]) {
  return request.post<ApiResponse<null>>(
    AuditMaterialApi.ASYNC_TO_PAIXIN,
    [...accessList],
    {
      baseURL: config.PAIXIN_SERVER_BASE_URL,
      isAuth: false,
    }
  )
}
